<?php
namespace Admin\Controller;
use Think\Controller;
//猜歌送红包 疯狂猜猜歌达人小超人GO
class GuessNewSongPlusController extends CommonController {
    const AppID     =   'wx370386b7eb0c04bc';
    const AppSecret =   '25d7798f008b06ec3641d5e709599600';

    public function index(){
        self::user_list();
    }

    //用户模块
    public function user_list()
    {
        $User       =   M('newsong_member'); // 实例化User对象
        $count      =   $User->count();// 查询满足要求的总记录数
        $Page       =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       =   $Page->show();// 分页显示输出

        $field      =   array('uid','nickname','avatarurl','province','city','game_num','prize_num','totle_num','money_total','total_money','lock_money','login_num','create_time','login_time');
        $data['user_list'] = $User->field($field)->order('uid desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);
        $this->display('user_list');
    }

    //更多好玩
    public function game_more()
    {
        $data['game_list']  =   M('newsong_app')->select();
        $this->assign("data",$data);
        $this->display('game_more');
    }

    public function game_more_add()
    {
        if (IS_AJAX) 
        {
            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envversion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('newsong_app')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_more_add');
        }
    }

    public function game_more_edit()
    {
        if (IS_AJAX) 
        {
            $where['newsong_app_id']  =   I('post.newsong_app_id','','htmlspecialchars');
            if (empty($where['newsong_app_id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['appid']     =   I('post.appid','','htmlspecialchars');
            $data['url']       =   I('post.url','','htmlspecialchars');
            $data['envversion']=   I('post.envversion','','htmlspecialchars');
            $data['logo']      =   I('post.logo','','htmlspecialchars');
            $data['content']   =   I('post.content','','htmlspecialchars');
            $data['isshow']    =   I('post.isshow','','htmlspecialchars');
            $data['title']     =   I('post.title','','htmlspecialchars');

            $result     =   M('newsong_app')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['newsong_app_id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['newsong_app_id']))
            {
                $this->error('非法请求');
            }

            $data['game_info']  =   M('newsong_app')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_more_edit');
        }
    }

    //游戏题库
    public function game_question()
    {
        $Module         =   M('newsong_questions'); // 实例化User对象
        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $field          =   array();
        $data['game_question'] = $Module->field($field)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);
        $this->display('game_question');
    }

    public function game_question_add()
    {
        if (IS_AJAX) 
        {
            $data['audio']      =   I('post.audio','','htmlspecialchars');
            $data['title']      =   I('post.title','','htmlspecialchars');
            $data['song']       =   I('post.song','','htmlspecialchars');
            $data['singer']     =   I('post.singer','','htmlspecialchars');
            $data['picture']    =   I('post.picture','','htmlspecialchars');
            $data['number']     =   I('post.number','','htmlspecialchars');
            $data['option']     =   I('post.option','','htmlspecialchars');
            $data['music_time'] =   I('post.music_time','','htmlspecialchars');
            $data['aswer_id']   =   I('post.aswer_id','','htmlspecialchars');

            $result     =   M('newsong_questions')->data($data)->add(); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '添加成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '添加失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $this->display('game_question_add');
        }
    }

    public function game_question_edit()
    {

        if (IS_AJAX) 
        {
            $where['id']  =   I('post.id','','htmlspecialchars');
            if (empty($where['id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');    
            }

            $data['audio']      =   I('post.audio','','htmlspecialchars');
            $data['title']      =   I('post.title','','htmlspecialchars');
            $data['song']       =   I('post.song','','htmlspecialchars');
            $data['singer']     =   I('post.singer','','htmlspecialchars');
            $data['picture']    =   I('post.picture','','htmlspecialchars');
            $data['number']     =   I('post.number','','htmlspecialchars');
            $data['option']     =   I('post.option','','htmlspecialchars');
            $data['music_time'] =   I('post.music_time','','htmlspecialchars');
            $data['aswer_id']   =   I('post.aswer_id','','htmlspecialchars');


            $result     =   M('newsong_questions')->where($where)->save($data); // 根据条件更新记录

            //记录相关操作日志

            if (!empty($result)) 
            {
                $msg['state']   =   200;
                $msg['error']   =   '更新成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '更新失败';
                $this->ajaxReturn ($msg,'JSON');
            }
        }
        else
        {
            $where['id']  =   I('get.id','','htmlspecialchars');

            if (empty($where['id']))
            {
                $this->error('非法请求');
            }

            $data['game_question']  =   M('newsong_questions')->where($where)->find();

            if ( empty($data)) 
            {
                $this->error('数据不存在');
            }

            $this->assign("data",$data);
            $this->display('game_question_edit');
        }
    }

    //分享微信群
    public function wechat_group()
    {
        $Module         =   M('newsong_wechat_group'); // 实例化对象
        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $field          =   array();
        $order_by['id'] =   'desc';
        $data['wechat_group']       =   $Module->field($field)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);

        $this->display('wechat_group');
    }

    //礼物领取记录(红包)
    public function game_redbag()
    {
        $Module         =   M('newsong_redbag'); // 实例化对象
        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $field          =   array('challenge_id','red_total','create_time','update_time','status','total_sn','member_id');
        $order_by['challenge_id']   =   'desc';
        $data['newsong_redbag']     =   $Module->field($field)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('page',$show);// 赋值分页输出
        $this->assign("data",$data);

        $this->display('game_redbag');
    }

    /**
     * 推送消息
     */
    public function push_message()
    {
        $data_num   =   $this->message_queue();

        if (!empty($data_num))
        {
            $client = new \swoole_client(SWOOLE_SOCK_TCP);
            //连接到服务器
            if (!$client->connect('127.0.0.1', 9501, 0.5))
            {
                //$this->write("connect failed.");
            }

            $data			=	array();
            $data['game']	=	'guessnewsong';
            $data['action'] =	'Push';

            $msg 			=	serialize($data);

            //向服务器发送数据
            if (!$client->send($msg))
            {
                //$this->write("send ".$msg." failed.");
            }

            //关闭连接
            $client->close();
        }
    }

    /**
     * @return int|string
     * 数据压入redis 队列
     */
    private function message_queue()
    {
        $num            =   0;
        $template_data 	=	M('newsong_template_send')->select();
        if(! empty($template_data))
        {
            $redis = new \Redis();

            $redis->connect('127.0.0.1',6379);

            $access_token = $this->gettoken(self::AppID,self::AppSecret);

            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";

            $tem = "K21rIUZuOoy_uz7jUInB9KAarQEdvlZS8XkzomYQ_Lg";

            $fdata = array(
                'keyword1'=>array(
                    'value'=>date('Y-m-d'),
                    'color'=>'#173177',
                ),
                'keyword2'=>array(
                    'value'=>'王俊凯来看你了',
                    'color'=>'#ff0000',
                ),
                'keyword3'=>array(
                    'value'=>'王源也来了',
                    'color'=>'#173177',
                ),

            );

            foreach($template_data as $key=> $value)
            {
                $data = array(
                    'touser'=>$value['touser'],
                    'template_id'=>$tem,
                    'page'=>'pages/index/index',
                    'form_id'=>$value['form_id'],
                    'data'=>$fdata,
                    'emphasis_keyword'=>'keyword2.DATA'
                );

                $post_data 			=	array();
                $post_data['data']	=	$data;
                $post_data['url']	=	$url;

                $redis->rPush('push_message_guessnewsong',json_encode($post_data));

                $key++;
                $num    =   $key;
            }

        }

        return $num;
    }

    //审核中
    public function game_cash()
    {

        if (IS_AJAX){

            $where['id'] = I('post.id','','htmlspecialchars');

            if (empty($where['id']))
            {
                $msg['state']   =   -1;
                $msg['error']   =   '非法请求';
                $this->ajaxReturn ($msg,'JSON');
            }

            $user = M('newsong_cash')->field('uid,money')->where($where)->find();

            $uid = $user['uid'];

            $upchsh = M('newsong_cash')->where($where)->save(array('cash_status'=>'1'));
             M('newsong_member')->where(array('uid'=>$uid))->save(array('lock_money'=>'0'));
            M('newsong_member')->where(array('uid'=>$uid))->setInc('used_money',$user['money']);



            if (!empty($upchsh))
            {
                $msg['state']   =   200;
                $msg['error']   =   '成功';
                $this->ajaxReturn ($msg,'JSON');
            }
            else
            {
                $msg['state']   =   -1;
                $msg['error']   =   '失败';
                $this->ajaxReturn ($msg,'JSON');
            }


        }else{

        $Module         =   M('newsong_cash'); // 实例化对象
        $where['cash_status'] = 0;
        $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash');
        }
    }

    //审核过
    public function game_cash1()
    {

        $Module         =   M('newsong_cash'); // 实例化对象
        $where['cash_status'] = 1;
        $count          =   $Module->where($where)->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->where($where)->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash1');
    }

    //所有
    public function game_cash2()
    {

        $Module         =   M('newsong_cash'); // 实例化对象

        $count          =   $Module->count();// 查询满足要求的总记录数
        $Page           =   new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show           =   $Page->show();// 分页显示输出

        $order_by['id']   =   'asc';
        $data     =   $Module->order($order_by)->limit($Page->firstRow.','.$Page->listRows)->select();


        $this->assign('page',$show);// 赋值分页输出

        $this->assign("data",$data);
        $this->display('game_cash2');
    }

}